package com.example.myapplication.ContentTest;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import androidx.fragment.app.FragmentPagerAdapter;
import com.example.myapplication.R;
import java.util.ArrayList;

import static androidx.fragment.app.FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT;

public class FragmentStatePagerAdapter extends FragmentPagerAdapter {
  private ArrayList<Fragment> fragmentArrayList;
  private Context context;

  public FragmentStatePagerAdapter(Context context,FragmentManager fragmentManager, ArrayList<Fragment> data) {
    super(fragmentManager,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    fragmentArrayList=data;
    this.context=context;
  }

  @NonNull @Override public Fragment getItem(int position) {
    return fragmentArrayList.get(position);
  }

  @Override public int getCount() {
    return fragmentArrayList.size();
  }

  @Nullable @Override public CharSequence getPageTitle(int position) {

    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
    //Fragment fragment = getItem(position);
    //返回每个Tab的标题，可根据需要修改
    if (position == 0) {
      //return "联系人信息";
      Drawable icon = context.getDrawable(R.drawable.baseline_chat_24);
      icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
      spannableStringBuilder.append(" ");
      ImageSpan imageSpan = new ImageSpan(icon, ImageSpan.ALIGN_BOTTOM);
      //添加空格，留出空间

      spannableStringBuilder.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      //添加换行符
      spannableStringBuilder.append("\n");
      //可以添加说明文字
      spannableStringBuilder.append("联系人信息");

    } else if (position == 1) {
      //return "修改联系人";
      Drawable icon = context.getDrawable(R.drawable.baseline_chat_24);
      icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
      spannableStringBuilder.append(" ");
      ImageSpan imageSpan = new ImageSpan(icon, ImageSpan.ALIGN_BOTTOM);

      spannableStringBuilder.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      spannableStringBuilder.append("\n");
      spannableStringBuilder.append("修改联系人");

    } else {
      return "未定义页面";
    }
    return spannableStringBuilder;
  }
}
